package com.example.chatroom.api;

import com.example.chatroom.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MessageSessionAPI {
    @Autowired
    private MessageSessionMapper messageSessionMapper;

    @Autowired
    private MessageMapper messageMapper;

    @GetMapping("/sessionList")
    public Object getSessionList(@SessionAttribute("user") User user) {
        // 这是返回的结果
        List<MessageSession> messageSessionList = new ArrayList<>();

        // 首先先得到该登录用户已有的 session 的 sessionId
        List<Integer> sessionIdList =
                messageSessionMapper.getSessionIdByUserId(user.getUserId());

        // 返回结果: sessionId
        //          friends(List)
        //          lastMessage
        for (int sessionId : sessionIdList) {
            MessageSession messageSession = new MessageSession();
            messageSession.setSessionId(sessionId);
            List<Friend> friends =
                    messageSessionMapper.getFriendsBySessionId(sessionId, user.getUserId());
            messageSession.setFriends(friends);
            String lastMessage = messageMapper.getLastMessageBySessionId(sessionId);
            messageSession.setLastMessage(lastMessage == null ?
                    "" : lastMessage);
            messageSessionList.add(messageSession);
        }
        return messageSessionList;
    }

    // 点击好友的时候创建的会话, 数据库中要添加进去
    // 这里涉及到两个表的操作, 当其中一个表操作失败时候要回滚
    @PostMapping("/session")
    @Transactional
    public Object addMessageSession(@SessionAttribute("user") User user, int toUserId) {
        // 这边这个 api 需要的返回值是 sessionId, 这边就不另外构造一个类了, 直接返回一个 HashMap, Jackson 也能将其转换为 json 对象
        Map<String, Integer> resp = new HashMap<>();
        MessageSession messageSession = new MessageSession();
        // 1. 创建会话时 新增 message_session 表
        messageSessionMapper.addMessageSession(messageSession);
        // 上述操作就可以获取到 sessionId 到 messageSession 中了

        // 2. 新增 message_session_user 表
        // a) 将自己添加到当前会话中
        MessageSessionUser user1 = new MessageSessionUser();
        user1.setSessionId(messageSession.getSessionId());
        user1.setUserId(user.getUserId());
        messageSessionMapper.addMessageSessionUser(user1);

        // b) 将点击的好友添加到当前会话中
        MessageSessionUser user2 = new MessageSessionUser();
        user2.setSessionId(messageSession.getSessionId());
        user2.setUserId(toUserId);
        messageSessionMapper.addMessageSessionUser(user2);

        System.out.println("[addMessageSession] 新增会话成功! sessionId = " + messageSession.getSessionId()
                + " userId1 = " + user.getUserId() + " userId2 = " + toUserId);

        resp.put("sessionId", messageSession.getSessionId());
        return resp;
    }

}
